package com.jinshan.handler;

import com.jinshan.exception.EduException;
import com.jinshan.response.RetVal;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获Exception异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public RetVal error(Exception e) {
        System.out.println("通用异常处理");
        e.printStackTrace();
        return RetVal.error().message("通用异常处理");
    }

    /**
     * 捕获全局的ArithmeticException异常
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public RetVal error(ArithmeticException e) {
        System.out.println("特殊异常处理逻辑");
        e.printStackTrace();
        return RetVal.error().message("特殊异常处理");
    }

    /**
     * 捕获自定义异常
     * @param eduException
     * @return
     */
    @ExceptionHandler(EduException.class)
    @ResponseBody
    public RetVal error(EduException eduException) {
        eduException.printStackTrace();
        System.out.println("自定义异常类");
        return RetVal.error().message(eduException.getMessage());
    }
}
